//
//  AVDTopBarTool.m
//  AVD_Demo
//
//  Created by 3tee on 2022/11/16.
//

#import "AVDTopBarTool.h"

@implementation AVDTopBarTool{
    UILabel *_roomLab;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    
    self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
    CGFloat padding = 15;
    CGFloat magin = 10;
    
    //扬声器
    UIButton *speakerBtn = [self buttonWithTitle:@"扬声器" imgNorName:@"" selName:@"" selectorName:@"speakerBtnClick:"];
    speakerBtn.frame = CGRectMake(padding, 0, 60, CGRectGetHeight(self.frame));
    [self addSubview:speakerBtn];
    
    //房间号
    UILabel *roomLab = [[UILabel alloc]initWithFrame:CGRectMake((CGRectGetWidth(self.frame)-160)/2, 0, 160, CGRectGetHeight(self.frame))];
    roomLab.text = @"12345678980";
    roomLab.textAlignment = NSTextAlignmentCenter;
    roomLab.textColor = [UIColor blackColor];
    [self addSubview:roomLab];
    _roomLab = roomLab;
    //扬声器
    UIButton *leaveBtn = [self buttonWithTitle:@"离开" imgNorName:@"" selName:@"" selectorName:@"leaveBtnClick"];
    leaveBtn.frame = CGRectMake(CGRectGetWidth(self.frame) - padding -60, 0, 60, CGRectGetHeight(self.frame));
    [self addSubview:leaveBtn];
}

- (UIButton *)buttonWithTitle:(NSString *)title imgNorName:(NSString *)name selName:(NSString *)selName selectorName:(NSString *)selectorName{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //[btn setBackgroundColor:[UIColor cyanColor]];
    [btn setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:selName] forState:UIControlStateSelected];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:17];
    //[btn setImageLayout:UIButtonLayoutImageTop space:5];
    [btn addTarget:self action:NSSelectorFromString(selectorName) forControlEvents:UIControlEventTouchUpInside];
    //btn.isSizeToFit = true;
    return  btn;
}

- (void)leaveBtnClick{
    if(self.leaveBlock){
        self.leaveBlock();
    }
}

- (void)speakerBtnClick:(UIButton *)btn{
    btn.selected = !btn.selected;
    if(self.speakerBlock){
        self.speakerBlock(btn.isSelected);
    }
}

- (void)updateRoomId:(NSString *)roomId{
    _roomLab.text = roomId;
}

@end
